package com.qfedu.web;
import com.qfedu.dao.CartDao;
import com.qfedu.model.Cart;
import com.qfedu.model.CartItem;
import com.qfedu.model.MilkTea;
import com.qfedu.model.User;
import com.qfedu.service.CartItemService;
import com.qfedu.service.CartService;
import com.qfedu.service.MilkTeaMeauService;
import com.qfedu.service.impl.CartItemServiceImpl;
import com.qfedu.service.impl.CartServiceImpl;
import com.qfedu.service.impl.MilkTeaMeauServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author 王海龙
 * @date 2023-04-18
 */
@WebServlet("/cartServlet")
public class CartServlet extends BaseServlet{
    private MilkTeaMeauService mtms = new MilkTeaMeauServiceImpl();
    private CartService cartService = new CartServiceImpl();
    private  CartItemService  cartItemService = new CartItemServiceImpl();
    //添加购物车
    public  void  addCartItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        User user = (User) req.getSession().getAttribute("user");
        //判断是否登录
        if (user!=null){
            //已经登陆
            Cart cart = cartService.getCart(user);
            //判断购物车是否存在
            if (cart!=null){
                //购物车存在
                //添加商品
                String id = req.getParameter("tid");
                int id1 = Integer.parseInt(id);//商品ID
                MilkTea milkTea = mtms.findMilkTeaById(id1);
                if (milkTea!=null){
                    //创建商品栏  7个字段元素
                    CartItem cartItem = new CartItem();
                    cartItem.setId(milkTea.getTid());
                    cartItem.setName(milkTea.getTname());
                    cartItem.setPrice(milkTea.getPrice());
                    cartItem.setCount(1);
                    cartItem.setTotalPrice(milkTea.getPrice());
                    //购物车
                    CartItem cartItem1 = cart.getItems().get(cartItem.getId());
                    //判断购物车中是否有该商品
                    if (cartItem1!=null){
                        //存在该商品合并
                        int count = cartItem1.getCount()+1;
                        cartItem.setCount(count);
                        int row = cartItemService.updateCartItem(cartItem);
                        if (row>0){
                            cart= cartService.getCart(user);//更新购物车
                        }
                    }else {
                        //不存在该商品
                        cartItem.setCid(cart.getCid());
                        int row2 = cartItemService.addCartItme(cartItem);
                        if (row2 > 0) {
                            cart = cartService.getCart(user);//更新购物车
                        }
                    }
                    req.getSession().setAttribute("cart",cart);
                    req.getRequestDispatcher("/pages/cart/cart.jsp").forward(req,resp);
                }else {
                    //商品不存在
                    req.getSession().setAttribute("message","商品不存在，请重新添加！");
                    req.getRequestDispatcher("error.jsp").forward(req,resp);
                }

            }else {
                //购物车不存在
                req.getSession().setAttribute("message","系统忙！");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
            }
        }else {
            //没有登陆
            req.getSession().setAttribute("message","还未登陆，请登录！");
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        }
    }
    //绑定购物车
    public  void  getCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        User user = (User) req.getSession().getAttribute("user");
        if (user!=null){
            Cart cart = cartService.getCart(user);
            if (cart==null){
                req.setAttribute("message","系统正忙");
                req.getRequestDispatcher("/error.jsp").forward(req,resp);
            }else {
                req.getSession().setAttribute("cart",cart);
                req.getRequestDispatcher("/pages/cart/cart.jsp").forward(req,resp);
            }

        }else {
            resp.sendRedirect("/pages/user/login.jsp");
        }
    }
    //清空购物车
    public  void  clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart!=null){
            int cid = cart.getCid();
            int row = cartItemService.truncate(cid);
            if (row==0){
                req.getSession().setAttribute("message","清空失败！");
                resp.sendRedirect("/error.jsp");
            }else {
                resp.sendRedirect("/cartServlet?action=getCart");
            }
        }
    }
    //删除购物车里的指定商品栏
    public void deleteItem(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart!=null){
            int row = cartItemService.deleteItem(i);
            if (row>0){
                resp.sendRedirect("/cartServlet?action=getCart");
            }else {
                req.getSession().setAttribute("message","删除失败！");
                resp.sendRedirect("/error.jsp");
            }
        }
    }
    //修改商品栏数量
    public  void  updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int id = Integer.parseInt(req.getParameter("id"));
        int count = Integer.parseInt(req.getParameter("count"));//新的数量
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        Map<Integer, CartItem> items = cart.getItems();
        CartItem cartItem = items.get(id);
        cartItem.setCount(count);
        int row = cartItemService.updateCartItem(cartItem);
        if (row>0){
            resp.sendRedirect("/pages/cart/cart.jsp");
        }
        }
}
